Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

birpc

Package Overview
Dependencies
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

birpc

Message based Two-way remote procedure call

  • 0.2.19
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is birpc?

The 'birpc' npm package is a lightweight library for creating bidirectional RPC (Remote Procedure Call) communication between different contexts, such as between a web worker and the main thread, or between an iframe and its parent window. It simplifies the process of sending and receiving messages, handling responses, and managing errors.

What are birpc's main functionalities?

Basic RPC Communication

This feature allows you to set up basic RPC communication between two contexts. In this example, the main thread defines an 'add' function that can be called from the worker.

const { createBirpc } = require('birpc');

// In the main thread
const rpc = createBirpc({
  add: (a, b) => a + b
}, {
  post: (msg) => worker.postMessage(msg),
  on: (fn) => worker.onmessage = (e) => fn(e.data)
});

// In the worker
const rpc = createBirpc({}, {
  post: (msg) => postMessage(msg),
  on: (fn) => onmessage = (e) => fn(e.data)
});

// Call the 'add' function from the worker
rpc.add(1, 2).then(result => console.log(result)); // 3

Error Handling

This feature demonstrates how to handle errors in RPC communication. The main thread defines a function that throws an error, and the worker catches and logs the error message.

const { createBirpc } = require('birpc');

// In the main thread
const rpc = createBirpc({
  throwError: () => { throw new Error('Something went wrong'); }
}, {
  post: (msg) => worker.postMessage(msg),
  on: (fn) => worker.onmessage = (e) => fn(e.data)
});

// In the worker
const rpc = createBirpc({}, {
  post: (msg) => postMessage(msg),
  on: (fn) => onmessage = (e) => fn(e.data)
});

// Call the 'throwError' function from the worker
rpc.throwError().catch(err => console.error(err.message)); // 'Something went wrong'

Bidirectional Communication

This feature shows how to set up bidirectional communication, where both the main thread and the worker can define and call functions on each other.

const { createBirpc } = require('birpc');

// In the main thread
const rpc = createBirpc({
  mainFunction: () => 'Hello from main thread'
}, {
  post: (msg) => worker.postMessage(msg),
  on: (fn) => worker.onmessage = (e) => fn(e.data)
});

// In the worker
const rpc = createBirpc({
  workerFunction: () => 'Hello from worker'
}, {
  post: (msg) => postMessage(msg),
  on: (fn) => onmessage = (e) => fn(e.data)
});

// Call the 'workerFunction' from the main thread
rpc.workerFunction().then(result => console.log(result)); // 'Hello from worker'

// Call the 'mainFunction' from the worker
rpc.mainFunction().then(result => console.log(result)); // 'Hello from main thread'

Other packages similar to birpc

Keywords

FAQs

Package last updated on 08 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc